/**
  ******************************************************************************
  * @file    kea128_uart.c
  * @author  huaxicunzhang
  * @version V1.0
  * @date    15-8-2015
  * @brief   This file contains all the functions prototypes for the USART 
  *          firmware library.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

#include"kea128_uart.h"

void UART_Init(UART_Type* UARTx, UART_InitTypeDef* UART_InitStruct)
{
		uint8_t Temp;
		uint16_t Sbr;			

		ASSERT(UARTx ==UART0||UARTx ==UART1||UARTx ==UART2);
		ASSERT(IS_USART_WORD_LENGTH(UART_InitStruct->UART_WordLength));
		ASSERT(IS_UART_STOPBITS(UART_InitStruct->UART_StopBits));
		ASSERT(IS_USART_PARITY(UART_InitStruct->UART_Parity));
		ASSERT(IS_USART_MODE(UART_InitStruct->UART_Mode));
		ASSERT(IS_UART_PIN(UART_InitStruct->UART_PIN));
		Sbr = (((SystemBusClock)>>4) + (UART_InitStruct->UART_BaudRate>>1))/UART_InitStruct->UART_BaudRate;  
		if(UARTx == UART0)
		{
			SIM_SCGC |= SIM_SCGC_UART0_MASK;    //开启UART0时钟 
			//选择RX、TX端口
			if(UART_InitStruct->UART_PIN)
				SIM_PINSEL0 |=SIM_PINSEL_UART0PS_MASK;          //RX PTA2 TX PTA3
			else
				SIM_PINSEL0 |= (~SIM_PINSEL_UART0PS_MASK);			//RX PTB0	TX PTB1		
		}
		else if(UARTx == UART1)
		{
			SIM_SCGC |= SIM_SCGC_UART1_MASK;    //开启UART1时钟 
			//选择RX、TX端口
			if(UART_InitStruct->UART_PIN)
				SIM_PINSEL1 |=SIM_PINSEL1_UART1PS_MASK;          //RX PTF2 TX PTF3
			else
				SIM_PINSEL1 |= (~SIM_PINSEL1_UART1PS_MASK);			//RX PTC6	TX PTC7		
		}
		else
		 {
			 SIM_SCGC |= SIM_SCGC_UART2_MASK;    //开启UART2时钟 
			 //选择RX、TX端口
				if(UART_InitStruct->UART_PIN)
					SIM_PINSEL1 |=SIM_PINSEL1_UART2PS_MASK;          //RX PTI0 TX PTI1
				else
					SIM_PINSEL1 |= (~SIM_PINSEL1_UART2PS_MASK);			//RX PTD6	TX PTD7			
		 }
		
		//禁止收发器
		UARTx->C2  &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK ); 
		UARTx->C1  = 0;
		 
		//数据长度和校验
		 
		UARTx->C1  |= (UART_InitStruct->UART_WordLength|UART_InitStruct->UART_Parity);
		
		//设置波特率
		Temp = UARTx->BDH & ~(UART_BDH_SBR_MASK);	
		
		UARTx->BDH = Temp |  UART_BDH_SBR(Sbr >> 8);
		 
		//停止位
		UARTx->BDH |= UART_InitStruct->UART_StopBits ;
		 
		UARTx->BDL = (u8)(Sbr & UART_BDL_SBR_MASK);
		 
		//接收发送使能
		UARTx->C2 |= UART_InitStruct->UART_Mode ;
		
		
}
/**
  * @brief  Enables or disables the specified USART interrupts.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0,UART1, UART2, 
  * @param  USART_IT: specifies the UART interrupt sources to be enabled or disabled.
  *   This parameter can be one of the following values:

  *     @arg UART_IT_TXE:  Transmit Data Register empty interrupt
  *     @arg UART_IT_TC:   Transmission complete interrupt
  *     @arg UART_IT_RXNE: Receive Data register not empty interrupt
  *     @arg UART_IT_IDLE: Idle line detection interrupt
  *     @arg UART_IT_ORE:  receive overrun interrupt 
  *     @arg UART_IT_NERR:  noise error interrupt
  *     @arg UART_IT_PE:    Parity Error interrupt
  *     @arg UART_IT_ERR:   framing error interrupt
  * @param  NewState: new state of the specified USARTx interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */

void UART_ITConfig(UART_Type* UARTx,UART_InterruptType UART_IT, FunctionalState NewState)
{
	 ASSERT((UARTx == UART0) || (UARTx == UART1) || (UARTx == UART2));
	if(NewState == ENABLE)
	{
		 UARTx->C2 |= UART_IT;
  }
	else
	{
		 UARTx->C2 &= ~UART_IT;
	}
	
}

/**
  * @brief  Checks whether the specified USART flag is set or not.
  * @param  UARTx: Select the USART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1, UART2,
  * @param  USART_FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
  *     @arg UART_Flag_PF:  Parity error flag
  *     @arg UART_Flag_FE:  Framing error flag
  *     @arg UART_Flag_TDRE:  Transmit data register empty flag
  *     @arg UART_Flag_TC:   Transmission Complete flag
  *     @arg UART_FLAG_RXNE: Receive data register not empty flag
  *     @arg UART_Flag_IDLE: Idle Line detection flag
  *     @arg UART_Flag_ORE:  OverRun Error flag
  *     @arg UART_Flag_RAF:   Receiver active flag
  *     @arg UART_Flag_LBKDE:   LIN break detection enable
  *     @arg UART_Flag_BRK13:   Break character generation length
	*     @arg UART_Flag_RWUID:   Receive wake up idle detect
	*     @arg UART_Flag_RXINV:   Receive data inversion
	*     @arg UART_Flag_Rev1:    Reserved
	*     @arg UART_Flag_RXEDGIF:   RxD pin active edge interrupt flag 
	*     @arg UART_Flag_LBKDIF:   LIN break detect interrupt flag
  * @retval The new state of UART_FLAG (SET or RESET).
  */
FlagStatus UART_GetFlagStatus(UART_Type* UARTx, UART_FlagType UART_FLAG)
{
	  FlagStatus bitstatus = RESET;
	  uint16_t u16StatusFlags = 0;
    u16StatusFlags = UARTx->S2;
    u16StatusFlags = (u16StatusFlags<<8)| UARTx->S1; 
	  if((u16StatusFlags & (1<< UART_FLAG)) != (uint16_t)RESET)
			bitstatus = SET;
		else bitstatus =RESET;
		return bitstatus;
}


/*
 * 函数名：UART_SendData
 * 描述  ：UART发送一字节数据
 * 输入  ：-UART_Type
 *         -UART0 UART1 UART2
 *         -c 数据
 *         -
 * 输出  ：无
 * 返回  ：无
 * 调用  ：被USART_printf()调用
 */
void UART_SendData(UART_Type* UARTx,unsigned char c)
{
	 ASSERT((UARTx == UART0) || (UARTx == UART1) || (UARTx == UART2));
		if(UARTx==UART0) 
		{
			while(!(UART0_S1 & UART_S1_TDRE_MASK));
			UART0_D = c;		
		}
		else if(UARTx==UART1) 
		{
			while(!(UART1_S1 & UART_S1_TDRE_MASK));
			UART1_D = c;		
		}
		else
		{
			while(!(UART2_S1 & UART_S1_TDRE_MASK));
			UART2_D = c;		
		}
}
void UART_SendStr(UART_Type* UARTx,char *buff)
{
    while((0 != *buff))
    {
			UART_SendData(UARTx,*buff++);
			while(UART_GetFlagStatus(UARTx,UART_Flag_TC) !=RESET);   
    }
			
}

